<?php
/**
 * Initialize Cache, and associate it to main Zend Objects
 * @see Zend_Cache
 */
class BRail_Application_Resource_Cache extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * Get cache according to options and associatie to main Zend objects like Zend_Local
     * @see Zend_Cache
     * @return Zend_Cache_Frontend
     */
    public function init()
    {
        if (Zend_Registry::isRegistered('Zend_Cache')) {
            return Zend_Registry::get('Zend_Cache');
        }

        $cache = $this->getCache();

        Zend_Locale::setCache($cache);
        Zend_Translate::setCache($cache);
        Zend_Currency::setCache($cache);
        Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);

        Zend_Registry::set('Zend_Cache', $cache);

        return $cache;
    }

    /**
     * Get cache according to options
     * @return Zend_Cache_Frontend
     */
    public function getCache()
    {
        $options = $this->getOptions();
        return Zend_Cache::factory($options['frontEnd']['type'],
                                   $options['backEnd']['type'],
                                   $options['frontEnd']['options'],
                                   $options['backEnd']['options']
        );
    }
}